package com.mcxx.result;

import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;


@Data
public class BaseResult<T> {

    @JsonView(BaseResultView.class)
    private String code = "0";
    @JsonView(BaseResultView.class)
    private String msg = "success";
    @JsonView(BaseResultView.class)
    private T data;

    public static interface BaseResultView {

    }

    public BaseResult(T data) {
        this.code = String.valueOf(ResultCode.SUCCESS.getCode());
        this.msg = ResultCode.SUCCESS.getMsg();
        this.data = data;
    }

    public BaseResult() {
        this.code = String.valueOf(ResultCode.SUCCESS.getCode());
        this.msg = ResultCode.SUCCESS.getMsg();
    }

    public BaseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static BaseResult fail() {
        return fail(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg());
    }

    public static <T> BaseResult<T> fail(String msg) {
        return fail(ResultCode.FAIL.getCode(), msg);
    }

    public static <T> BaseResult<T> fail(String code, String msg) {
        BaseResult<T> r = new BaseResult<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> BaseResult<T> success() {
        return new BaseResult<>();
    }

    public static <T> BaseResult<T> success(T t) {
        BaseResult<T> r = new BaseResult<>();
        r.setData(t);
        return r;
    }

    public boolean isSuccess() {
        return this.code.equals(ResultCode.SUCCESS.getCode());
    }

    public boolean isFail() {
        return !this.code.equals(ResultCode.SUCCESS.getCode());
    }
}
